#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Módulos 
import sys, gtk, pygame, utils, objetos
from pygame.locals import *

# Constantes

# Clases
# ---------------------------------------------------------------------
class Avion(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)
        self.image = utils.load_image("images/c3_1_avion.png",True)
        self.rect = self.image.get_rect()
        self.rect.centerx = 1000*utils.RES
        self.rect.centery = 160*utils.RES
        self.vx = -3
        self.sonido = pygame.mixer.Sound("sounds/avioneta.ogg")
        self.sonido.set_volume(0.05)   
    def mover(self):
		if self.rect.centerx < -200*utils.RES:
			self.rect.centerx = 1000*utils.RES
		else:
			self.rect.centerx = self.rect.centerx + self.vx
		    
class Linea:
	def __init__(self):
		
		self.linea,self.linea_rect
		self.posx_fin = posx
		self.posy_fin = posy        
   

# ---------------------------------------------------------------------
def dibujar(screen,cursor_picture, clock):
    salir = 0
    background_image = utils.load_image('images/fondo_3.png',True)
    i= 0
    j= 0
    numero_linea = 1
    btn_adelante = objetos.BotonAdelante()
    btn_atras = objetos.BotonAtras()
    
    
    clase, clase_rect = utils.texto_clase("Clase 3", 740*utils.RES, 30*utils.RES)
           
    titulo, titulo_rect = utils.texto_subtitulo("¿ADÓNDE VA LA BASURA?", 200*utils.RES, 60*utils.RES)
    titulo_sub, titulo_sub_rect = utils.texto_subtitulo("....................................", 200*utils.RES, 70*utils.RES)
    basurometro, basurometro_rect = utils.texto_subtitulo_azul("BASURÓMETRO", 110*utils.RES, 240*utils.RES,(255,0,0))
    
    frame = 0
    avion = Avion()
    fondo_basura = utils.load_image('images/c3_1_fondo_basura.jpg')
    alarma = utils.load_image('images/c3_1a.png',True)
    sonido_alarma = pygame.mixer.Sound("sounds/alarma.ogg")
    sonido_alarma.set_volume(0.2)   
    sonido_alarma.play(-1)	
    avion.sonido.play(-1)	
    show_alarma = False
    while salir == 0:
    
    	# Pump GTK messages.
		while gtk.events_pending():
			gtk.main_iteration()
			
		for eventos in pygame.event.get():
			if eventos.type == QUIT:
				sys.exit(0)
			elif eventos.type == pygame.VIDEORESIZE:
			   pygame.display.set_mode(eventos.size, pygame.RESIZABLE)	
			elif eventos.type == MOUSEBUTTONDOWN:
				if btn_adelante.rect.collidepoint(pygame.mouse.get_pos()):
					salir = 2
					btn_adelante.sonido.play()	
				elif btn_atras.rect.collidepoint(pygame.mouse.get_pos()):
					salir = -1					
					btn_atras.sonido.play()	

		# Realiza la animacion del texto
		
		avion.mover()
			
		pygame.draw.rect(screen,(255,255,255),(700*utils.RES,25*utils.RES,100*utils.RES,70*utils.RES))
		screen.blit(fondo_basura, (28*utils.RES, 38*utils.RES))	
		screen.blit(avion.image, avion.rect)
		screen.blit(background_image, (0, 0))
		if frame % 14 == 0:
			if show_alarma:
				show_alarma = False
			else:
				show_alarma = True
		if show_alarma:	
			screen.blit(alarma, (40, 250))
		screen.blit(clase, clase_rect)
		screen.blit(titulo, titulo_rect)
		screen.blit(titulo_sub, titulo_sub_rect)
		screen.blit(basurometro, basurometro_rect)
		screen.blit(btn_adelante.image, btn_adelante.rect)
		screen.blit(btn_atras.image, btn_atras.rect)
		screen.blit(cursor_picture, pygame.mouse.get_pos())
		pygame.display.flip()

		# Try to stay at 30 FPS
		clock.tick(30)
		frame = frame +1
    sonido_alarma.stop()	
    avion.sonido.stop()	            	 
    return salir

# This function is called when the game is run directly from the command line:
# ./TestGame.py 
def main():
	
	pygame.init()
	screen = pygame.display.set_mode((int(800*utils.RES), int(520*utils.RES)), pygame.RESIZABLE) #pygame.display.get_surface() #

	# Fake a Sugar cursor for the pyGame canvas area
	pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
	cursor_picture = pygame.image.load('images/standardcursor.png')
	cursor_picture.convert_alpha() 
	clock = pygame.time.Clock()
	dibujar(screen,cursor_picture,clock)

if __name__ == '__main__':
   main()
